import bs4, requests, os, shutil

url = 'https://www.cqut.edu.cn/'
html = requests.get(url)
print('网页下载中.')
html.raise_for_status()
print('网页下载完成')
destDir ='out_images'
if os.path.exists(destDir) == True:
    shutil.rmtree(destDir)
os.mkdir(destDir)
objSoup = bs4.BeautifulSoup(html.text,  'lxml')
imgTag = objSoup.select('img')
print('搜寻到的图片数量=',len(imgTag))
if len(imgTag)> 0:
    for i in range(len((imgTag))):
        imgUrl = imgTag[i].get('src')
        print('%s 图片下载...' % imgUrl)
        finUrl = url + imgUrl
        print('%s 图片下载中...' % finUrl)
        picture = requests.get(finUrl)
        picture.raise_for_status()
        print("%s 图片下载成功" % finUrl)

        pictFile = open(os.path.join(destDir, os.path.basename(imgUrl)), 'wb')
        for diskStorage in picture.iter_content(10240):
            pictFile.write(diskStorage)
        pictFile.close()